package pl.un4giv3n.mightvsmagic.action;

import javax.persistence.EntityManager;

import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.utils.FightUtil;
import pl.un4giv3n.mightvsmagic.utils.ImageUtil;
import pl.un4giv3n.mightvsmagic.utils.StatsUtil;
import pl.un4giv3n.mightvsmagic.utils.UserStats;

/**
 * @author andrzejewskia
 * shows the user profile
 */
public class ShowUserAction extends PersistenceAction {

	private static final long serialVersionUID = 1L;
	/**
	 * user to show, this is populated by struts from request params
	 */
	private Long id;
	/**
	 * user from the db selected by login
	 */
	private User user;
	private UserStats stats;

	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = entityManager.find(User.class, id);
		if(user.isBlocked()){
			addActionError("Wybrany gracz nie istnieje");
			entityManager.close();
			return ERROR;
		}
		stats = StatsUtil.getFreshUserStats(user, entityManager);
		entityManager.close();
		return SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public double getLife() {
		return user.getHp();
	}

	/**
	 * @return true if this user can be attacked
	 * used to show the attack link on the players profile
	 */
	public boolean getCanAttack() {
		return FightUtil.canDefend(user);
	}
	

	public String getUserAvatar(){
		return ImageUtil.getUserAvatar(user, getContext().getRealPath("/"));
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public UserStats getStats() {
		return stats;
	}
	
	public String getUserType(){
		return getText(user.getUserType().name());
	}
	

}
